package com.wanxi.service.impl;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanxi.VO.UserVO;
import com.wanxi.dao.UserDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.entity.UserEntity;
import com.wanxi.service.UserService;
import com.wanxi.util.Md5;
import com.wanxi.util.Verify;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/23 22:37
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private  UserDao userDao;
    @Autowired
    private  UserEntity userEntity;
    Jedis jedis = new Jedis();

    /**
     * 删除指定数据
     * @param userVO
     * @return
     */
    public ResultDTO delete(UserVO userVO) {
//        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userVO,userEntity);
        int delete = userDao.delete(userEntity);
        jedis.del("userList");
        return ResultDTO.getResult(delete);
    }
    /**
     * 修改数据
     * @param userVO
     * @return
     */
    public ResultDTO update(UserVO userVO) {
//        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userVO,userEntity);
        int update = userDao.update(userEntity);
        jedis.del("userList");
        return ResultDTO.getResult(update);
    }

    /**
     * 修改用户，修改用户之前通过id把用户的数据查询出来；
     * @param userVO
     * @return
     */
    public ResultDTO findById(UserVO userVO) {
//        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userVO,userEntity);
//        List<UserEntity> byId = userDao.findById(userEntity);
        UserEntity byId = userDao.findById(userEntity);
        return ResultDTO.getResult(byId);
    }
    /**
     * 根据条件查询所有满足条件的用户
     * @param userVO
     * @return
     */
    public ResultDTO findAll(UserVO userVO) {
//        UserEntity userEntity = new UserEntity();

        ObjectMapper mapper = new ObjectMapper();
        BeanUtils.copyProperties(userVO,userEntity);
        int count = userDao.count(userEntity);
        List<UserEntity> list = new ArrayList<>();

        list = userDao.findAll(userEntity);
        try {
            String userList = mapper.writeValueAsString(list);
            jedis.set("userList",userList);
            if (userEntity.getPage() != null && userEntity.getPage() == 1 && jedis.exists("userList")){
                String jedisUserList = jedis.get("userList");
                //反序列化数组，方法2
                //factory:工厂,construct:建设，parameter:参数，parametric：参数化，type:类型,exists:存在
                JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, UserEntity.class);
                try {
                    list = mapper.readValue(jedisUserList, javaType);
                    jedis.del("userList");
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        System.out.println("List<UserEntity>："+list);
        return ResultDTO.getResult("", count, list);
    }


    /**
     * 登录验证用户名是不是存在数据库
     * @param userVO username password
     * @return
     */
    public ResultDTO login(UserVO userVO) {
        UserEntity userEntity = new UserEntity();
        ResultDTO resultDTO = new ResultDTO();
        //        判断验证码是否正确
        if (userVO.getCode().equals(userVO.getServletContextCode())) {
//            UserEntity userEntity = new UserEntity();
            //如果想要正确的拷贝，必须要保证里面的字段名相同
            BeanUtils.copyProperties(userVO,userEntity);
//        对密码进行加密？
            String anwen = Md5.encoderByMd5(userEntity.getPassword());
            userEntity.setPassword(anwen);
            String username = userEntity.getUsername();
            //验证密码和用户是否在数据库用户表存在；
            userEntity = userDao.verifyNameAndPassword(userEntity);
            if (jedis.exists(username) && Integer.parseInt(jedis.get(username))>3){
                return Verify.passwordWrongThreeTimes(username);
            }
            if (userEntity == null){
                jedis.setnx(username,"1");
                jedis.incr(username);
                if (Integer.parseInt(jedis.get(username))>3){
                    jedis.expire(username,86400);
                }
                //error:错误
                return ResultDTO.getResult("error：密码或用户名错误请重新输入");
            }else {
                //success:成功
                return ResultDTO.getResult("success");
            }
        }else {
            resultDTO.setCode(-1);
            resultDTO.setMsg("error:验证码错误");
        }
        return resultDTO;
    }

    public ResultDTO add(UserVO userVO) {
//        UserEntity userEntity = new UserEntity();
        //        对密码进行加密？
//        String mingwen = userEntity.getPassWord();
//        String anwen = Md5.encoderByMd5(mingwen);
//        userEntity.setPassWord(anwen);
        //        如果想要正确的拷贝，必须要保证里面的字段名相同
        BeanUtils.copyProperties(userVO, userEntity);
        //        对密码进行加密？
        String anwen = Md5.encoderByMd5(userEntity.getPassword());
        userEntity.setPassword(anwen);


        UserEntity nameComparison = userDao.addNameComparison(userEntity);
        System.out.println("a是什么东西"+nameComparison);
        //判读用户名是否重名，重名就取消创建
        if (nameComparison != null){
            return ResultDTO.getResult(2);
        }
        jedis.del("userList");
        return ResultDTO.getResult(userDao.add(userEntity));
    }






}
